package com.simoniu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

//连接MySQL
public class MySQLJdbcDemo {

    public static void main(String[] args) {

        //程咬金的三板斧。
        //1.加载驱动。
        String driverClass = "com.mysql.cj.jdbc.Driver";
        //2.配置用户名和密码
        String username = "root";
        String password = "root";

        //3 配置数据库的URL
        String url = "jdbc:mysql://localhost:3306/myschool?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true";
        Connection conn = null;
        try {
            //加载驱动
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url,username,password);
            if(conn!=null){
                System.out.println("MySQL连接成功！");
            }else{
                System.out.println("MySQL连接失败！");
            }

        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
